package com.rslai.commons.util.xmind;

import com.rslai.commons.util.StringUtil;
import com.rslai.commons.util.xmind.enums.StructureEnum;
import com.rslai.commons.util.xmind.enums.MarkerEnum;
import com.rslai.commons.util.xmind.pojo.Attached;
import org.xmind.core.*;

import java.io.IOException;
import java.util.List;

/**
 * 写入 XMind 文件
 */
public class XmindWrite {

    /**
     * 写入 XMind 文件
     * @param filePath XMind文件路径
     * @param structureEnum 结构类型（XMind 显示样式）
     * @param attached 节点的信息
     * @return
     * @throws CoreException
     * @throws IOException
     */
    public boolean write(String filePath, StructureEnum structureEnum, Attached attached) throws CoreException, IOException {
        // 创建思维导图的工作空间
        IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
        IWorkbook workbook = workbookBuilder.createWorkbook();


        ISheet primarySheet = workbook.getPrimarySheet(); // 获得默认sheet

        ITopic rootTopic = primarySheet.getRootTopic(); // 获得根主题
        setTopicInfo(workbook, rootTopic, attached); // 设置根主题的内容
        rootTopic.setStructureClass(structureEnum.getValue()); // 设置根主题的结构类型（XMind 显示样式）

        recursivelyBuildSubtopic(workbook,rootTopic,attached.getChildren()); // 递归创建子主题

        workbook.save(filePath); // 保存 xmind
        return true;
    }

    /**
     * 递归创建子主题
     * @param workbook
     * @param parentTopic
     * @param children
     * @return
     */
    private boolean recursivelyBuildSubtopic(IWorkbook workbook, ITopic parentTopic, List<Attached> children) throws IOException {
        for (Attached attached : children) {
            ITopic sonTopic = workbook.createTopic(); // 创建一个子主题
            parentTopic.add(sonTopic, ITopic.ATTACHED); // 将子主题添加到父主题下

            setTopicInfo(workbook, sonTopic, attached); // 设置子主题的内容

            // 设置该节点的子节点
            if (attached.getChildren() != null) {
                recursivelyBuildSubtopic(workbook, sonTopic, attached.getChildren()); // 递归创建子主题
            }
        }

        return true;
    }

    /**
     * 设置一个主题的内容
     * @param workbook workbook
     * @param topic topic 对象
     * @param attached 当前节点的信息
     * @return
     */
    private boolean setTopicInfo(IWorkbook workbook, ITopic topic, Attached attached) throws IOException {
        topic.setTitleText(attached.getTitle()); // 设置子主题的标题

        // 添加备注内容
        if (StringUtil.isNotBlank(attached.getNotes())) {
            IPlainNotesContent plainContent = (IPlainNotesContent) workbook.createNotesContent(INotes.PLAIN);
            plainContent.setTextContent(attached.getNotes());
            INotes notes = topic.getNotes();
            notes.setContent(INotes.PLAIN, plainContent);
        }

        // 添加该节点的 图标（marker）
        if (attached.getMarkers() != null) {
            for (MarkerEnum markerId : attached.getMarkers()) {
                topic.addMarker(markerId.getValue());
            }
        }

        topic.setFolded(attached.isFolded()); // 设置该节点是否折叠

        // 设置该节点的标签（label）
        if (attached.getLabels() != null) {
            topic.setLabels(attached.getLabels());
        }

        // 设置该节点的图片
        if (attached.getImage() != null && attached.getImage().getSrc() != null) {
            // 先创建 attachment 对象，将图片添加到 attachment 目录下
            IManifest manifest = workbook.getManifest();
            IFileEntry entry = manifest.createAttachmentFromFilePath(attached.getImage().getSrc());

            // 创建 image 对象
            IImage image = topic.getImage();
            image.setSource("xap:" + entry.getPath()); // 设置图片路径
            if (attached.getImage().getHeight() != 0) {
                image.setHeight(attached.getImage().getHeight()); // 设置图片高度
            }
            if (attached.getImage().getWidth() != 0) {
                image.setWidth(attached.getImage().getWidth()); // 设置图片宽度
            }
            if (attached.getImage().getAlignment() != null) {
                image.setAlignment(attached.getImage().getAlignment().getValue()); // 设置图片对齐方式
            }
        }
        return true;
    }
}
